package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Imagesize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 裁图尺寸
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/imagesize")
public class AimagesizeController extends BaseController {

    // 裁图尺寸列表
    @RequestMapping
    public String index() {
        List<Imagesize> imagesizes = factorySession.createCriteria(Imagesize.class).addOrder(Order.asc("id"))
                .list();
        request.setAttribute("imagesizes", imagesizes);
        return view();
    }

    // 裁图尺寸编辑
    @RequestMapping("edit")
    public String edit() {
        int imagesizeId = request.getParameter("imagesizeid") == null ? 0 :
                Integer.parseInt(request.getParameter("imagesizeid"));
        Imagesize imagesize = imagesizeId == 0 ? new Imagesize() : factorySession.get(Imagesize.class, imagesizeId);
        request.setAttribute("imagesize", imagesize);
        return view();
    }

    // 裁图尺寸编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_systemsetup_imagesize");
        int imagesizeId = Integer.parseInt(request.getParameter("imagesizeid"));
        Imagesize imagesize = imagesizeId == 0 ? new Imagesize() :
                factorySession.load(Imagesize.class, imagesizeId, LockMode.PESSIMISTIC_WRITE);
        imagesize.setWidth(Integer.parseInt(request.getParameter("width")));
        imagesize.setHeight(Integer.parseInt(request.getParameter("height")));
        imagesize.setDescription(request.getParameter("description"));
        if (imagesizeId == 0) factorySession.save(imagesize);
        recordLog((imagesizeId == 0 ? "新增" : "修改") + "裁图尺寸《" + imagesize.getDescription().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 裁图尺寸删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_systemsetup_imagesize");
        int imagesizeId = Integer.parseInt(request.getParameter("imagesizeid"));
        Imagesize imagesize = factorySession.load(Imagesize.class, imagesizeId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(imagesize);
        recordLog("删除裁图尺寸《" + imagesize.getDescription().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
